home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / h8300-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-03  |  9.3 KB  |  396 lines

  1. /* Target-machine dependent code for Hitachi H8/300, for GDB.
  2.    Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* 
  21.  Contributed by Steve Chamberlain
  22.                 sac@cygnus.com 
  23.  */
  24.  
  25. #include "defs.h"
  26. #include "frame.h"
  27. #include "obstack.h"
  28. #include "symtab.h"
  29. #define UNSIGNED_SHORT(X) ((X) & 0xffff)
  30.  
  31. /* an easy to debug H8 stack frame looks like:
  32. 0x6df2    push    r2
  33. 0x6df3    push    r3
  34. 0x6df6    push    r6
  35. 0x    mov.w    r7,r6
  36.     subs    stuff,sp  mov.w #x,r5
  37.                       subs  r5,sp
  38.  
  39.  */
  40.  
  41. #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
  42. #define IS_MOVE_FP(x) (x == 0x0d76)
  43. #define IS_MOV_SP_FP(x) (x == 0x0d76)
  44. #define IS_SUB2_SP(x) (x==0x1b87)
  45. #define IS_MOVK_R5(x) (x==0x7905)
  46. CORE_ADDR examine_prologue();
  47.  
  48. void   frame_find_saved_regs ();
  49. CORE_ADDR h8300_skip_prologue(start_pc)
  50. CORE_ADDR start_pc;
  51.  
  52. {
  53.  
  54.   /* Skip past all push insns */
  55.   short int w;
  56.   
  57.   w = read_memory_short(start_pc);
  58.   while (IS_PUSH(w)) 
  59.   {
  60.     start_pc+=2;  
  61.     w = read_memory_short(start_pc);
  62.   }
  63.  
  64.   /* Skip past a move to FP */
  65.   if (IS_MOVE_FP(w)) {
  66.       start_pc +=2 ;
  67.       w = read_memory_short(start_pc);
  68.     }
  69.  
  70.   return start_pc;  
  71.   
  72. }
  73.  
  74.  
  75. int
  76. print_insn(memaddr, stream)
  77. CORE_ADDR memaddr;
  78. FILE *stream;
  79. {
  80.   /* Nothing is bigger than 8 bytes */
  81.   char   data[8];
  82.   read_memory (memaddr, data, sizeof(data));
  83.   return print_insn_h8300(memaddr,  data, stream);
  84. }
  85.      
  86.      
  87. /* Given a GDB frame, determine the address of the calling function's frame.
  88.    This will be used to create a new GDB frame struct, and then
  89.    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
  90.  
  91.    For us, the frame address is its stack pointer value, so we look up
  92.    the function prologue to determine the caller's sp value, and return it.  */
  93.  
  94. FRAME_ADDR
  95. FRAME_CHAIN (thisframe)
  96.      FRAME thisframe;
  97. {
  98.  
  99.   frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
  100.     return thisframe->fsr->regs[SP_REGNUM];
  101. }
  102.  
  103.      
  104.  
  105. /* Put here the code to store, into a struct frame_saved_regs,
  106.    the addresses of the saved registers of frame described by FRAME_INFO.
  107.    This includes special registers such as pc and fp saved in special
  108.    ways in the stack frame.  sp is even more special:
  109.    the address we return for it IS the sp for the next frame.
  110.  
  111.    We cache the result of doing this in the frame_cache_obstack, since
  112.    it is fairly expensive.  */
  113.  
  114. void
  115. frame_find_saved_regs (fi, fsr)
  116.      struct frame_info *fi;
  117.      struct frame_saved_regs *fsr;
  118. {
  119.   register CORE_ADDR next_addr;
  120.   register CORE_ADDR *saved_regs;
  121.   register int regnum;
  122.   register struct frame_saved_regs *cache_fsr;
  123.   extern struct obstack frame_cache_obstack;
  124.   CORE_ADDR ip;
  125.   struct symtab_and_line sal;
  126.   CORE_ADDR limit;
  127.  
  128.   if (!fi->fsr)
  129.     {
  130.       cache_fsr = (struct frame_saved_regs *)
  131.           obstack_alloc (&frame_cache_obstack,
  132.                  sizeof (struct frame_saved_regs));
  133.       bzero (cache_fsr, sizeof (struct frame_saved_regs));
  134.       fi->fsr = cache_fsr;
  135.  
  136.       /* Find the start and end of the function prologue.  If the PC
  137.      is in the function prologue, we only consider the part that
  138.      has executed already.  */
  139.          
  140.       ip = get_pc_function_start (fi->pc);
  141.       sal = find_pc_line (ip, 0);
  142.       limit = (sal.end && sal.end < fi->pc) ? sal.end: fi->pc;
  143.  
  144.       /* This will fill in fields in *fi as well as in cache_fsr.  */
  145.       examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
  146.     }
  147.  
  148.   if (fsr)
  149.     *fsr = *fi->fsr;
  150. }
  151.      
  152.  
  153. /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
  154.    is not the address of a valid instruction, the address of the next
  155.    instruction beyond ADDR otherwise.  *PWORD1 receives the first word
  156.    of the instruction.*/
  157.  
  158.  
  159. CORE_ADDR
  160. NEXT_PROLOGUE_INSN(addr, lim, pword1)
  161. CORE_ADDR addr;
  162. CORE_ADDR lim;
  163. short *pword1;
  164. {
  165.   if (addr < lim+8)   
  166.   {
  167.     read_memory (addr, pword1, sizeof(*pword1));
  168.     SWAP_TARGET_AND_HOST (pword1, sizeof (short));
  169.     return addr + 2;
  170.   }
  171.  
  172.   return 0;
  173.  
  174. }
  175.  
  176. /* Examine the prologue of a function.  `ip' points to the first instruction.
  177.    `limit' is the limit of the prologue (e.g. the addr of the first 
  178.    linenumber, or perhaps the program counter if we're stepping through).
  179.    `frame_sp' is the stack pointer value in use in this frame.  
  180.    `fsr' is a pointer to a frame_saved_regs structure into which we put
  181.    info about the registers saved by this frame.  
  182.    `fi' is a struct frame_info pointer; we fill in various fields in it
  183.    to reflect the offsets of the arg pointer and the locals pointer.  */
  184.  
  185. /* We will find two sorts of prologue, framefull and non framefull:
  186.    
  187.    push   r2
  188.    push   r3
  189.    push   fp
  190.    mov    sp,fp
  191.    stack_ad
  192.  
  193.    and
  194.    push   x
  195.    push   y
  196.    stack_ad
  197.  
  198. */
  199.  
  200. static CORE_ADDR
  201. examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
  202.      register CORE_ADDR ip;
  203.      register CORE_ADDR limit;
  204.      FRAME_ADDR after_prolog_fp;
  205.      struct frame_saved_regs *fsr;
  206.      struct frame_info *fi;
  207. {
  208.   register CORE_ADDR next_ip;
  209.   int r;
  210.   int i;
  211.   int have_fp = 0;
  212.   
  213.   register int src;
  214.   register struct pic_prologue_code *pcode;
  215.   INSN_WORD insn_word;
  216.   int size, offset;
  217.   unsigned int reg_save_depth = 2; /* Number of things pushed onto
  218.                       stack, starts at 2, 'cause the
  219.                       PC is already there */
  220.  
  221.   unsigned int auto_depth = 0;    /* Number of bytes of autos */
  222.   
  223.   char in_frame[NUM_REGS];      /* One for each reg */
  224.   
  225.   memset(in_frame, 1, NUM_REGS);
  226.   
  227.   if (after_prolog_fp == 0) {
  228.       after_prolog_fp = read_register(SP_REGNUM);
  229.     }  
  230.   if (ip == 0 || ip & ~0xffff) return 0;
  231.  
  232.   next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
  233.  
  234.   /* Skip over any push instructions, and remember where they were saved */
  235.  
  236.  
  237.   while (next_ip && IS_PUSH(insn_word))
  238.   {
  239.     ip = next_ip;
  240.     in_frame[insn_word & 0x7] = reg_save_depth;
  241.     next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word);
  242.     reg_save_depth +=2;
  243.  
  244.   }
  245.   
  246.  
  247.   /* Is this a move into the fp */
  248.   if (next_ip && IS_MOV_SP_FP(insn_word)) 
  249.   {
  250.     ip = next_ip;
  251.     next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word);
  252.     have_fp = 1;
  253.     
  254.   }
  255.   
  256.  
  257.   /* Skip over any stack adjustment, happens either with a number of
  258.      sub#2,sp or a mov #x,r5 sub r5,sp */
  259.  
  260.   
  261.   if (next_ip && IS_SUB2_SP(insn_word))
  262.   {
  263.     while (next_ip && IS_SUB2_SP(insn_word)) 
  264.     {
  265.       auto_depth +=2 ;
  266.       ip = next_ip;
  267.       next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word);
  268.     }
  269.   }
  270.   else  
  271.   {
  272.     if (next_ip && IS_MOVK_R5(insn_word)) 
  273.     {
  274.       ip = next_ip;
  275.       next_ip = NEXT_PROLOGUE_INSN(ip, limit, &insn_word);
  276.       auto_depth += insn_word;
  277.       ip +=4;
  278.       
  279.     }
  280.   }
  281.  
  282.  
  283.   
  284.   /* The args are always reffed based from the stack pointer */
  285.   fi->args_pointer =  after_prolog_fp - auto_depth;
  286.   /* Locals are always reffed based from the fp */
  287.   fi->locals_pointer = after_prolog_fp ;
  288.   /* The PC is at a known place */
  289.   fi->from_pc = read_memory_short(after_prolog_fp + reg_save_depth-2 );
  290.   
  291.  
  292.   /* Rememeber any others too */
  293.  
  294.   in_frame[PC_REGNUM] = 0;
  295.   
  296.   for (r = 0; r < NUM_REGS; r++) 
  297.   {
  298.     if (in_frame[r] != 1) 
  299.     {
  300.       fsr->regs[r] = after_prolog_fp + reg_save_depth - in_frame[r] -2;
  301.     }
  302.     else
  303.     {
  304.       fsr->regs[r] = 0;
  305.     }
  306.   }    
  307.   if (have_fp) 
  308.    /* We keep the old FP in the SP spot */
  309.    fsr->regs[SP_REGNUM] = (read_memory_short(fsr->regs[6])) ;
  310.   else 
  311.    fsr->regs[SP_REGNUM] = after_prolog_fp + reg_save_depth;
  312.   
  313.   return (ip);
  314. }
  315.  
  316. void
  317. init_extra_frame_info (fromleaf, fi)
  318.      int fromleaf;
  319.      struct frame_info *fi;
  320. {
  321.   fi->fsr = 0;            /* Not yet allocated */
  322.   fi->args_pointer = 0;        /* Unknown */
  323.   fi->locals_pointer = 0;    /* Unknown */
  324.   fi->from_pc = 0;
  325.   
  326. }
  327. /* Return the saved PC from this frame.
  328.  
  329.    If the frame has a memory copy of SRP_REGNUM, use that.  If not,
  330.    just use the register SRP_REGNUM itself.  */
  331.  
  332. CORE_ADDR
  333. frame_saved_pc (frame)
  334. FRAME frame;
  335.  
  336. {
  337.   return frame->from_pc;
  338. }
  339.  
  340.  
  341. CORE_ADDR
  342. frame_locals_address (fi)
  343.      struct frame_info *fi;
  344. {
  345.   if (!fi->locals_pointer) 
  346.   {
  347.     struct frame_saved_regs ignore;
  348.     get_frame_saved_regs(fi, &ignore);  
  349.  
  350.   }
  351.   return fi->locals_pointer;
  352. }
  353.  
  354. /* Return the address of the argument block for the frame
  355.    described by FI.  Returns 0 if the address is unknown.  */
  356.  
  357. CORE_ADDR
  358. frame_args_address (fi)
  359.      struct frame_info *fi;
  360. {
  361.   if (!fi->args_pointer) 
  362.   {
  363.     struct frame_saved_regs ignore;
  364.   get_frame_saved_regs(fi, &ignore);  
  365.  
  366.   }
  367.   
  368.   return fi->args_pointer;
  369. }
  370.  
  371.  
  372. void h8300_pop_frame()
  373. {
  374.   unsigned regnum;
  375.   struct frame_saved_regs fsr;
  376.   struct frame_info *fi;
  377.  
  378.   FRAME frame = get_current_frame();  
  379.   fi = get_frame_info(frame);  
  380.   get_frame_saved_regs(fi, &fsr);
  381.  
  382.   for (regnum = 0; regnum < NUM_REGS; regnum ++) 
  383.   {
  384.     if(fsr.regs[regnum]) 
  385.     {
  386.       write_register(regnum, read_memory_short (fsr.regs[regnum]));
  387.     }
  388.   
  389.     flush_cached_frames();
  390.     set_current_frame(create_new_frame(read_register(FP_REGNUM),
  391.                        read_pc()));
  392.   
  393.   }
  394.  
  395. }
  396.